package org.zero.concurrency.demo.example.atomic;

import lombok.extern.slf4j.Slf4j;
import org.zero.concurrency.demo.annotations.ThreadSafe;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @program: concurrency-demo
 * @description:
 * @author: 01
 * @create: 2018-10-15 22:39
 **/
@Slf4j
@ThreadSafe
public class AtomicExample6 {
    /**
     * 执行总数
     */
    public static int execTotal = 5000;

    private static AtomicBoolean isHappened = new AtomicBoolean(false);

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < execTotal; i++) {
            executorService.execute(AtomicExample6::test);
        }

        executorService.shutdown();
        log.info("isHappened: {}", isHappened.get());
    }

    private static void test() {
        // 只会打印一次日志
        if (isHappened.compareAndSet(false, true)) {
            log.info("execute");
        }
    }
}
